package com.haoran.dwz.common.redis;

import java.io.Serializable;
import java.util.Date;
import java.util.concurrent.TimeUnit;

import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.support.atomic.RedisAtomicLong;

public class RedisSequenceFactory {
	RedisTemplate<String, Serializable> redisTemplate;

	public RedisSequenceFactory(RedisTemplate<String, Serializable> redisTemplate) {
		super();
		this.redisTemplate = redisTemplate;
	}

	/**
	 * @Title: set
	 * @Description: set cache.
	 * @param key
	 * @param value
	 * @param expireTime
	 */
	public void set(String key, int value, Date expireTime) {
		RedisAtomicLong counter = new RedisAtomicLong(key, redisTemplate.getConnectionFactory());
		counter.set(value);
		counter.expireAt(expireTime);
	}

	/**
	 * @Title: set
	 * @Description: set cache.
	 * @param key
	 * @param value
	 * @param timeout
	 * @param unit
	 */
	public void set(String key, int value, long timeout, TimeUnit unit) {
		RedisAtomicLong counter = new RedisAtomicLong(key, redisTemplate.getConnectionFactory());
		counter.set(value);
		counter.expire(timeout, unit);
	}

	/**
	 * @Title: generate
	 * @Description: Atomically increments by one the current value.
	 * @param key
	 * @return
	 */
	public long generate(String key) {
		RedisAtomicLong counter = new RedisAtomicLong(key, redisTemplate.getConnectionFactory());
		return counter.incrementAndGet();
	}
	
	/**
	 * 获取当前序列
	 * @param key
	 * @return
	 */
	public long get(String key) {
		RedisAtomicLong counter = new RedisAtomicLong(key, redisTemplate.getConnectionFactory());
		return counter.get();
	}

	/**
	 * @Title: generate
	 * @Description: Atomically increments by one the current value.
	 * @param key
	 * @return
	 */
	public long generate(String key, Date expireTime) {
		RedisAtomicLong counter = new RedisAtomicLong(key, redisTemplate.getConnectionFactory());
		counter.expireAt(expireTime);
		return counter.incrementAndGet();
	}
	
	/**
	 * @param key 序列值
	 * @param initialValue 初始化值
	 * @return
	 */
	public long init(String key, long initialValue) {
		RedisAtomicLong counter = new RedisAtomicLong(key, redisTemplate.getConnectionFactory(), initialValue);
		return counter.get();
	}

	/**
	 * @Title: generate
	 * @Description: Atomically increments by one the current value.
	 * @param key
	 * @return
	 */
	public long generate(String key, Date expireTime, long initialValue) {
		RedisAtomicLong counter = new RedisAtomicLong(key, redisTemplate.getConnectionFactory(), initialValue);
		counter.expireAt(expireTime);
		return counter.incrementAndGet();
	}
	
	/**
	 * @Title: generate
	 * @Description: Atomically adds the given value to the current value.
	 * @param key
	 * @param increment
	 * @return
	 */
	public long generate(String key, int increment) {
		RedisAtomicLong counter = new RedisAtomicLong(key, redisTemplate.getConnectionFactory());
		return counter.addAndGet(increment);
	}

	/**
	 * @Title: generate
	 * @Description: Atomically adds the given value to the current value.
	 * @param key
	 * @param increment
	 * @param expireTime
	 * @return
	 */
	public long generate(String key, int increment, Date expireTime) {
		RedisAtomicLong counter = new RedisAtomicLong(key, redisTemplate.getConnectionFactory());
		counter.expireAt(expireTime);
		return counter.addAndGet(increment);
	}
}
